﻿using System;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using Illusion.Elements.Xml;
using UnityEngine;

namespace Studio.Config
{
	// Token: 0x02000460 RID: 1120
	public abstract class BaseSystem : Data
	{
		// Token: 0x06001BFB RID: 7163 RVA: 0x000B3720 File Offset: 0x000B1920
		public BaseSystem(string elementName) : base(elementName)
		{
		}

		// Token: 0x170002D4 RID: 724
		// (get) Token: 0x06001BFC RID: 7164 RVA: 0x000B372C File Offset: 0x000B192C
		public FieldInfo[] FieldInfos
		{
			get
			{
				return base.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
			}
		}

		// Token: 0x06001BFD RID: 7165 RVA: 0x000B373C File Offset: 0x000B193C
		public override void Read(string rootName, XmlDocument xml)
		{
			string str = rootName + "/" + this.elementName + "/";
			foreach (FieldInfo fieldInfo in this.FieldInfos)
			{
				XmlNodeList xmlNodeList = xml.SelectNodes(str + fieldInfo.Name);
				if (xmlNodeList != null)
				{
					XmlElement xmlElement = xmlNodeList.Item(0) as XmlElement;
					if (xmlElement != null)
					{
						fieldInfo.SetValue(this, BaseSystem.Cast(xmlElement.InnerText, fieldInfo.FieldType));
					}
				}
			}
		}

		// Token: 0x06001BFE RID: 7166 RVA: 0x000B37D8 File Offset: 0x000B19D8
		public override void Write(XmlWriter writer)
		{
			writer.WriteStartElement(this.elementName);
			foreach (FieldInfo fieldInfo in this.FieldInfos)
			{
				writer.WriteStartElement(fieldInfo.Name);
				writer.WriteValue(BaseSystem.ConvertString(fieldInfo.GetValue(this)));
				writer.WriteEndElement();
			}
			writer.WriteEndElement();
		}

		// Token: 0x06001BFF RID: 7167 RVA: 0x000B383C File Offset: 0x000B1A3C
		public static object Cast(string str, Type type)
		{
			if (type == typeof(Color))
			{
				string[] array = str.Split(new char[]
				{
					','
				});
				if (array.Length == 4)
				{
					int num = 0;
					return new Color(float.Parse(array[num++]), float.Parse(array[num++]), float.Parse(array[num++]), float.Parse(array[num++]));
				}
				return Color.white;
			}
			else
			{
				if (type.IsArray)
				{
					string[] array2 = str.Split(new char[]
					{
						','
					});
					Type elementType = type.GetElementType();
					Array array3 = Array.CreateInstance(elementType, array2.Length);
					foreach (var callback in array2.Select((string v, int i) => new
					{
						v,
						i
					}))
					{
						array3.SetValue(Convert.ChangeType(callback.v, elementType), callback.i);
					}
					return array3;
				}
				return Convert.ChangeType(str, type);
			}
		}

		// Token: 0x06001C00 RID: 7168 RVA: 0x000B3978 File Offset: 0x000B1B78
		public static string ConvertString(object o)
		{
			if (o is Color)
			{
				Color color = (Color)o;
				return string.Format("{0},{1},{2},{3}", new object[]
				{
					color.r,
					color.g,
					color.b,
					color.a
				});
			}
			if (o.GetType().IsArray)
			{
				Array array = (Array)o;
				StringBuilder stringBuilder = new StringBuilder();
				for (int i = 0; i < array.Length; i++)
				{
					stringBuilder.Append(array.GetValue(i));
					if (i + 1 < array.Length)
					{
						stringBuilder.Append(",");
					}
				}
				return stringBuilder.ToString();
			}
			return o.ToString();
		}
	}
}
